package com.thenmoret.util;

import java.io.IOException;

import com.qiniu.common.QiniuException;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;

public class uploadUtil {

	// 设置好账号的ACCESS_KEY和SECRET_KEY
		private static String ACCESS_KEY = "GF6n5972vDfx_inYrClPaexiLip-QjtwAtOVs665";
		private static String SECRET_KEY = "w-Dhy7ZVt2OdV7sqeze1cloJvHx3SHZxENpmlcvf";
		// 要上传的空间
		private static String bucketname = "thenmore";

		// 密钥配置
		private static Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);

		// 简单上传，使用默认策略，只需要设置上传的空间名就可以了
		public static String getUpToken() {
			return auth.uploadToken(bucketname);
		}

		/**
		 * 
		 * @param ImgName 上传到七牛云的文件名
		 * @param FilePath 上传本地图片路径位置
		 * @throws IOException
		 */
		public static void upload(String ImgName,String FilePath) throws IOException {
			try {
				// 创建上传对象
				UploadManager uploadManager = new UploadManager();
				// 调用put方法上传
				com.qiniu.http.Response res = uploadManager.put(FilePath, ImgName,
						getUpToken());
				// 打印返回的信息
			} catch (QiniuException e) {
				com.qiniu.http.Response r = e.response;
				// 请求失败时打印的异常的信息
				try {
					// 响应的文本信息
					System.out.println(r.bodyString());
				} catch (QiniuException e1) {
					// ignore
				}
			}
		}
}
